var  userlocationsFO,neighborhoodsFO,allcitiesFO,allcatsFO,searchWhat,searchWhere;
var FO = new Array();

function initGlobalFlyouts() {
    var aLink = document.getElementsByTagName('a');
	var _TabNews = '';
	for(i=0;i<aLink.length;i++){
		if(aLink[i].className == 'news'){
			_TabNews = aLink[i].id;
			break;
		}
	}
	
//     if ($chk($(_TabNews))) {
//        FO[FO.length] = new FlyOutPane($('div_RecentNews'),$(_TabNews),{},
//                    {'pos' : {'x' : ['left','right',-110],'y' : ['top','bottom',0]}
//                });
//    }
    
    var aA = document.getElementsByTagName('a');
    for(i=0;i<aA.length;i++){
        if(aA[i].className == "prizea"){
            if($chk($(aA[i].id+'_p'))){
                FO[FO.length] = new FlyOutPane($(aA[i].id+'_p'),$(aA[i].id),{},
                    {'pos' : {'x' : ['left','right',-60],'y' : ['top','bottom',-230]}
                });
            }
        }
        
        if(aA[i].className == "topmenu"){
            if($chk($('p_'+aA[i].id))){
                FO[FO.length] = new FlyOutPane($('p_'+aA[i].id),$(aA[i].id),{},
                    {'pos' : {'x' : ['left','right',-80],'y' : ['top','bottom',0]}
                });
            }
        }
        if(aA[i].className == "dropdownarrow"){
            if ($chk($(aA[i].id))) {
                FO[FO.length] = new ClickHoverPosPane($(aA[i].id+"_div"),$(aA[i].id),{},
                    {'pos' : {'x' : ['left','right',-520],'y' : ['top','bottom',-4]}
                });
            }
        }
        if(aA[i].className == "dropdownarrowbubble"){
            if($chk($(aA[i].id))){
                FO[FO.length] = new FlyOutPane($(aA[i].id+"_div"),$(aA[i].id),{},
                    {'pos' : {'x' : ['left','right',-380],'y' : ['top','bottom',-4]}
                });
            }
        }
}
    if ($chk($("allcities-links")) && self.allCitiesLinks) {
        writeFlyoutChunkLinks($("allcities-links"), allCitiesLinks,
            function(el) { return li(span(el)); }, ul, 3);
    }
    if ($chk($("allcats-links")) && self.allCatsLinks) {
        writeFlyoutChunkLinks($("allcats-links"), allCatsLinks,
            function(el) { return li(span(el)); }, ul, 3);
    }

    if ($chk($("allcats-sections")) && self.allCatsSectionLinks) {
        writeFlyoutChunkLinks($("allcats-sections"), allCatsSectionLinks,
            function(el) { return li(span(el)); }, ul, 4);
    }
    if ($chk($("allcities-flyout"))) {
        allcitiesFO = new FlyOutPane($('allcities-flyout'),$('allcities-flyout-trigger'),{},
            {'pos' : {'x' : ['left','left',0],'y' : ['top','bottom',0]}
        });
    }

    if ($chk($("allcats-flyout"))) {
        allcatsFO = new FlyOutPane($('allcats-flyout'),$('allcats-flyout-trigger'),{},
            {'pos' : {'x' : ['left','right',-240],'y' : ['top','bottom',0]}
        });
    }
    if ($chk($("userlocation-dd"))) {
        userlocationsFO = new ClickHoverPosPane($("userlocation-dd"),$("userlocation-dd-trigger"),{},
            {'pos' : {'x' : ['left','right',-293],'y' : ['top','bottom',-9]}
        });
    }

    if ($chk($("hoods-fly-out"))) {
        if ($chk($("hoods-fly-out-trigger"))) {
            neighborhoodsFO = new FlyOutPane($("hoods-fly-out"),$("hoods-fly-out-trigger"),{},
                {'pos' : {'x' : ['left','left',0],'y' : ['top','bottom',0]}
            });
        }
        if ($chk($("hoods-fly-out-trigger2"))) {
            neighborhoodsFO = new FlyOutPane($("hoods-fly-out"),$("hoods-fly-out-trigger2"),{},
                {'pos' : {'x' : ['left','left',0],'y' : ['top','bottom',0]}
            });
        }
    }
    if ($chk($("search-what")) && $chk($("search-what-label"))) {
        searchWhat = new LabelMagic("search-what","search-what-label");
    }
    if ($chk($("search-where")) && $chk($("search-where-label"))) {
        searchWhere = new LabelMagic("search-where","search-where-label");
    }
}

pedro.event.addToOnLoad(initGlobalFlyouts);

var tooltips;

function initTips() {
    tooltips = new Tips($$('.tool-tip'), {
        maxTitleChars: 50,
        className : 'tt-bubble',
        offsets : { x : 20, y : 0 }
    });
}

/* Don't want to hold up the load with this, so after the dom loads */
window.addEvent("load", function() { initTips(); });

/* User Location */

var userLocation = {};

userLocation.changeLocation = function(location,formId) {
    var formEl = byId(formId);
    if (!isNull(formEl)) {
        formEl.where.value = (!isUndef(location.where)) ? location.where : "";
        formEl.geoId.value = (!isUndef(location.geoId)) ? location.geoId : "";
        formEl.submit();
    }
}

userLocation.clearRecentLocations = function(childId, parentId, baseProdHost){
    var childEl = byId(childId);
    var parentEl = byId(parentId);
    if(parentEl != null && childEl != null){
        parentEl.removeChild(childEl);
    }
    //deleting recent_locs cookie
    if(navigator.cookieEnabled){
        document.cookie = 'recent_locs=; domain=.'+baseProdHost;
    }
}
if(byId("leaderboard")){
    function initLeaderBoard() {
        if(byId("leaderboard").childNodes.length == 0) {
            byId("leaderboard").setStyle("display","none");
        }
    }
    pedro.event.addToOnLoad(initLeaderBoard);
}

function writeFlyoutChunkLinks(elem, links, itemFunc, chunkFunc, chunkSize)
{
    var chunks = chunkArray(links, chunkSize);
    for (var i=0; i<chunks.length; i++)
    {
        var chunkChild = elem.appendChild(chunkFunc());
        for (var j=0; j<chunks[i].length; j++)
        {
            chunkChild.appendChild(itemFunc(chunks[i][j]));
        }
    }
}

function writeFlyoutLinks(elem, links, itemFunc)
{
    for (var i=0; i<links.length; i++)
    {
        elem.appendChild(itemFunc(links[i]));
    }
}

